#region Usings

using System;
using KanbanBoard.Client.Model;
using KanbanBoard.Client.Model.Query;

#endregion

namespace KanbanBoard.Client.ViewModel.TaskBoardUseCase {
    public class TaskBoardViewModel : ViewModel {
        private readonly IContext context;
        private TaskBoard taskBoard;

        public TaskBoardViewModel(IContext context) {
            this.context = context;
            Columns = new ColumnRepository(context);

            context.Command.PullCompleted += (s, e) => Bind();
            context.Command.CreateTaskCompleted += (s, e) => Bind();
            context.Command.ChangeTaskDescriptionCompleted += (s, e) => Bind();
            context.Command.DeleteTaskCompleted += (s, e) => Bind();
            context.Command.PrioritizeAboveCompleted += (s, e) => Bind();
            context.Command.PrioritizeBelowCompleted += (s, e) => Bind();

            Bind();
        }

        private void Bind() {
            context.Query.GetTaskBoard(Bind);
        }

        private void Bind(TaskBoard taskBoard) {
            this.taskBoard = taskBoard;
            Columns.Bind(this.taskBoard);
        }

        public ColumnRepository Columns { get; private set; }

        public Action CreateNewTask { get { return OnCreateNewTask; }}

        private void OnCreateNewTask() {
            context.Command.CreateTaskAsync(taskBoard.Id);
        }
    }
}